package com.cy.pj.sys.controller;

import com.cy.pj.common.vo.JsonResult;
import com.cy.pj.sys.pojo.Role;
import com.cy.pj.sys.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@ResponseBody
public class RoleController {
    @Autowired
    private RoleService roleService;

    @RequestMapping("/role/page")
    public JsonResult getRolePage(
            // 注意：页面上有可能不提供关键词，要进行适配
            // 在@RequestParam注解中设置defaultValue属性为空字符串表示浏览器不提 供关键词时，keyword 变量赋值为空字符串
            @RequestParam(value="name",defaultValue="")String name,
            // 浏览器未提供 pageNum 时，默认前往第一页
            @RequestParam(value="pageNum",defaultValue="1")Integer pageNum,
            // 浏览器未提供 pageSize 时，默认每页显示 5 条记录
            @RequestParam(value="pageSize",defaultValue="3")Integer pageSize

    ){
        // 查询得到分页数据
        PageInfo<Role> rolePageInfo = roleService.selectRole(name, pageNum, pageSize);
        // 将分页数据存入模型
        /*modelMap.addAttribute(pageInfo);*/
        return new JsonResult(rolePageInfo);
    }
    @RequestMapping("/doDeleteRoleById")
    public JsonResult doDeleteRoleById(Integer[] ids){
        roleService.deleteRoleById(ids);
        return new JsonResult("Delete Ok");
    }
    @RequestMapping("/doSaveRole")
    public JsonResult doSaveRole(Role role){
        roleService.insertRole(role);
        return new JsonResult("Save Ok");
    }
    @RequestMapping("/findRoleById")
    public JsonResult findRoleById(Integer id){
        Role role = roleService.selectRoleById(id);
        return new JsonResult(role);

    }
}
